package com.qf.springbootproject.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.springbootproject.product.entity.ProductComments;
import com.qf.springbootproject.product.mapper.ProductCommentsMapper;
import com.qf.springbootproject.product.service.ProductCommentsService;
import com.qf.springbootproject.user.entity.Users;
import com.qf.springbootproject.user.service.UsersService;
import com.qf.springbootproject.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品评价  服务实现类
 * </p>
 *
 * @author yyq'
 * @since 2023-02-14
 */
@Service
public class ProductCommentsServiceImpl extends ServiceImpl<ProductCommentsMapper, ProductComments> implements ProductCommentsService {
    @Autowired
    UsersService usersService;

    /**
     * 商品评论
     *
     * @param id
     * @param pageNum
     * @param limit
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo productComments(Integer id, Integer pageNum, Integer limit) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        Page<ProductComments> page = new Page<>(pageNum, limit);
        //不需要返回值，查询出来的对象都在page里
        page(page, new QueryWrapper<ProductComments>().eq("product_id", id));
        List<ProductComments> records = page.getRecords();
        populate(records);
        ResultVo ok = ResultVo.ok();
        stringObjectHashMap.put("pageCount", page.getPages());
        stringObjectHashMap.put("count", page.getTotal());
        stringObjectHashMap.put("list", records);
        ok.put("data", stringObjectHashMap);
        return ok;
    }

    private void populate(List<ProductComments> records) {
        //1.List<ProductComments> -----> List<userId>
        List<String> userIdList = records.stream().map(productComments -> productComments.getUserId()).collect(Collectors.toList());
        //查询所有相关的用户信息
        List<Users> usersList = usersService.list(new QueryWrapper<Users>().in("user_id", userIdList));
        //遍历
        Map<Integer, Users> usersMap = usersList.stream().collect(Collectors.toMap(users -> users.getUserId(), users -> users));
        //4.遍历records,填充缺失信息
        for (ProductComments record : records) {
            String userId = record.getUserId();
            Users users = usersMap.get(Integer.valueOf(userId));
            record.setUsername(users.getUsername());
            record.setNickname(users.getNickname());
            record.setUserImg(users.getUserImg());
        }

    }

    /**
     * 商品好评率
     *
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo rating(Integer id) {
        //按照comm_type分组查询
        List<Map<String, Object>> listMaps = listMaps(new QueryWrapper<ProductComments>().select("comm_type,COUNT(1) as count").eq("product_id", id).groupBy("comm_type"));
        //2.遍历listMaps获取各评价信息
        Long goodTotal = 0l;
        Long badTotal = 0l;
        Long midTotal = 0l;
        for (Map<String, Object> listMap : listMaps) {
            Integer comm_type = (Integer) listMap.get("comm_type");
            if (comm_type.equals(1)) {
                //好评
                goodTotal = (Long) listMap.get("count");
            } else if (comm_type.equals(0)) {
                //中评
                badTotal = (Long) listMap.get("count");
            } else {
                //差评
                midTotal = (Long) listMap.get("count");
            }
        }
        //总评价数
        Long total = goodTotal + badTotal + midTotal;
        //好评率
        BigDecimal percent = BigDecimal.valueOf(goodTotal).multiply(BigDecimal.valueOf(100)).divide(BigDecimal.valueOf(total), 2, BigDecimal.ROUND_HALF_UP);
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("goodTotal", goodTotal);
        stringObjectHashMap.put("total", total);
        stringObjectHashMap.put("badTotal", badTotal);
        stringObjectHashMap.put("midTotal", midTotal);
        stringObjectHashMap.put("percent", percent);
        ResultVo ok = ResultVo.ok();
        ok.put("data", stringObjectHashMap);
        return ok;
    }
}
